package com.yphone.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class JsonUtil {
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static <T> T parserTFromJson(String pJsonStr, Class<T> pClass) {
        try {
            if (!TextUtils.isEmpty(pJsonStr)) {
                return new Gson().fromJson(pJsonStr, pClass);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Object parsData(String json, Class clazz) {
        try {
            return new Gson().fromJson(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Object parsData(String json, TypeToken type) {
        try {
            return new Gson().fromJson(json, type.getType());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Object parsData(String json, Type type) {
        try {
            return new Gson().fromJson(json, type);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String objectToString(Object o) {
        try {
            return new Gson().toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> Object parsListData(String json, Class clazz) {
        try {
            return new Gson().fromJson(json, new TypeToken<List<T>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Gson getGson() {
        return new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    }

    public static <T> List<T> json2Collection(String jsonStr, Class<T> cls) {
        return (List) getGson().fromJson(jsonStr, new TypeToken<List<T>>() {
        }.getType());
    }

    public static <T> List<T> getObjectList(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<>();
        try {
            Gson gson = new Gson();
            Iterator it = new JsonParser().parse(jsonString).getAsJsonArray().iterator();
            while (it.hasNext()) {
                list.add(gson.fromJson((JsonElement) it.next(), cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Object StringToDate(Object o) {
        try {
            return new Gson().toJson(o);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String class2Data(Object object) {
        try {
            return new Gson().toJson(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String class2DataToHtml(Object object) {
        try {
            return new GsonBuilder().disableHtmlEscaping().create().toJson(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}